package com.ht.controller;

import com.alibaba.fastjson.JSON;
import com.ht.bean.*;
import com.ht.common.TreeView;
import com.ht.common.Treejiebie;
import com.ht.service.PermissionService;
import com.ht.service.StudentService;
import com.ht.service.StypetingService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller//开启注解
@RequestMapping("/juris")
public class PermissionController {
    //权限Dao
    @Resource
    private PermissionService perm;

    //系统设置Dao
    @Resource
    private StypetingService styp;

    @Resource
    private StudentService stu;

    //权限模块——————————————————————————————————————————
    //跳转至权限管理
    @RequestMapping("/listquan")
    public String goquan(){
        return "quanxian";
    }
    //查询权限角色
    @RequestMapping("/juese")
    @ResponseBody
    public Map litchre(int page, int limit, Characters characters){
        List<Characters> data = perm.litchare((page - 1) * limit,limit,characters);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",perm.countchare(characters));//总页数
        map.put("data",data);//数据
        return map;
    }
    //去添加权限角色
    @RequestMapping("/toaddchre")
    public String toaddchre(Model model){
        List<Post> pos = perm.lidpost();
        model.addAttribute("pos",pos);
        return "quanxianadd";
    }

    //新增权限角色
    @RequestMapping("/addchare")
    @ResponseBody
    public Map addchare(Characters characters){
        Map map = new HashMap();
        if (perm.addchare(characters)){
            map.put("data","添加成功");
        }else {
            map.put("data","添加失败");
        }
        return map;
    }
    //删除角色
    @RequestMapping("/delcher")
    @ResponseBody
    public Map delcher(Integer characterId){
        Map map = new HashMap();
        if (perm.delchare(characterId)){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }
    //@PathVariable("stuId") Integer stuId
    //去编辑权限
    @RequestMapping("/shuzhuang/{characterId}/{characterName}")
    public String listshu(@PathVariable("characterId") int characterId, @PathVariable("characterName") String characterName,HttpSession session){
        session.setAttribute("characterId",characterId);
        session.setAttribute("characterName",characterName);
        return "quanxianup";
    }

    //编辑权限完成
    @RequestMapping("/zhuangtuli")
    @ResponseBody
    public Map zhuangtuli(HttpSession session){
        System.out.println("........"+(Integer)session.getAttribute("characterId"));
        Integer characterId  =  (Integer)session.getAttribute("characterId");
        System.out.println(characterId+"........");
        Map lis = new HashMap();
        Map map = new HashMap();
        List<Module> list= perm.modlit();
        List<Integer> checkedId= perm.litquan(characterId);
        List<Integer> disabledId = new ArrayList<>();
        disabledId.add(0);
        //树状图的数据
        lis.put("list",list);
        //已经选中的数据
        lis.put("checkedId",checkedId);
        //禁止用户选择的选项
        lis.put("disabledId",disabledId);
        //树状图的头
        map.put("code",0);
        map.put("msg","获取成功");
        map.put("data",lis);
        return map;
    }

    //提交编辑角色信息
    @RequestMapping("/qunxianxui")
    @ResponseBody
    public Map qunxianxui(@RequestParam Map data,HttpSession session){
        Map map = new HashMap();
//        System.out.println(",,,,,乌拉,,,,,,"+data.toString());
        Integer i = null;
        List<Charmodule> charmodulee= new ArrayList<>();
        Characters characters = new Characters();
        //从作用域中取角色表的主键
        Integer characterId  =  (Integer)session.getAttribute("characterId");
        //给表赋值
        characters.setCharacterId(characterId);
        characters.setCharacterName((String) data.get("name"));
//        //查询系统权限表数量
        int pei = perm.modactu();
        //修改权限角色的名字
        if (perm.upchare(characters)) {
            ////删除角色权限表中的系统权限数据，并查询角色系统权限数量
            if (perm.litquann(characterId) == 0 || perm.delmodeid(characterId)) {
                //查询系统权限表数量并循环
                for (int j = 0; j <= pei; j++) {
                    if (data.get("authids[" + j + "]") != "" && data.get("authids[" + j + "]") != null) {
                        Charmodule charmodule = new Charmodule();
                        charmodule.setCharacterId(characterId);
                        Integer moduleId = Integer.parseInt((String) data.get("authids[" + j + "]"));
                        charmodule.setModuleId(moduleId);
                        charmodulee.add(charmodule);
                        //执行
//                        System.out.println(">>>"+charmodule);
//                        System.out.println("<<<<<"+charmodulee);
                    }

                }
                if (perm.indmodel(charmodulee)) {
                    //修改完权限重新获取用户权限
                    session.setAttribute("empquan",styp.inquire((Integer) session.getAttribute("empID")));
//                    System.out.println("新增角色系统权限成功");
                }
            }
        }
        map.put("data","sb");
        return map;
    }

    //@PathVariable("stuId") Integer stuId
    //去修改用户角色表页面
    @RequestMapping("/tosouyon/{characterId}")
    public String tosouyon(@PathVariable("characterId") int characterId, HttpSession session){
        session.setAttribute("characterId",characterId);
        return "quanxianemp";
    }

    //用户树状图获取
    @RequestMapping("/yonhushou")
    @ResponseBody
    public Map yonhushou(HttpSession session){
//        System.out.println("........"+(Integer)session.getAttribute("characterId"));
        Integer characterId  =  (Integer)session.getAttribute("characterId");
//        System.out.println(characterId+"........");
        Map lis = new HashMap();
        Map map = new HashMap();
        List<Map> list= new ArrayList<>();
        //拼接树状图的所需的数据
        List<Dep> a = perm.litdepduijian();
        List<Emp> b = perm.litempduijian();
        for (Dep dep:a){
            Map map1 = new HashMap();
            map1.put("moduleId",dep.getDepid());
            map1.put("root",dep.getParentId());
            map1.put("moduleName",dep.getDepName());
            list.add(map1);
        }
        for (Emp emp:b){
            Map map1 = new HashMap();
            map1.put("moduleId",emp.getEmpId());
            map1.put("root",emp.getDepId());
            map1.put("moduleName",emp.getEmpName());
            list.add(map1);
        }
        //将树状图的数据条数传入作用域
        session.setAttribute("shuliang",list.size());
        List<Integer> checkedId= perm.litchempid(characterId);
        //禁止用户选择的选项
        List<Integer> disabledId = new ArrayList<>();
        disabledId.add(0);
        //树状图的数据
        lis.put("list",list);
        //已经选中的数据
        lis.put("checkedId",checkedId);
        //禁止用户选择的选项
        lis.put("disabledId",disabledId);
        //树状图的头
        map.put("code",0);
        map.put("msg","获取成功");
        map.put("data",lis);
        return map;
    }

    @RequestMapping("/souyonhu")
    @ResponseBody
    public Map souyonhu(@RequestParam Map data,HttpSession session){
        Map map = new HashMap();
//        System.out.println(",,,,,乌拉,,,,,,"+data.toString());
        Integer i = null;

        List<Charemp> charempp = new ArrayList<>();
        Characters characters = new Characters();
        //获取树状图的数据条数
        Integer shuliang  =  (Integer)session.getAttribute("shuliang");
        Integer characterId  =  (Integer)session.getAttribute("characterId");

        //取角色表的主键
        characters.setCharacterId(characterId);
        characters.setCharacterName((String) data.get("name"));

        int pei = (Integer)session.getAttribute("characterId");
            if (perm.delcemp(characterId)){
                for (int j=0;j<=shuliang;j++){
                    if (data.get("authids["+j+"]")!= "" && data.get("authids["+j+"]")!= null){
                        Charemp charemp =new  Charemp();
                        charemp.setCharacterId(characterId);
                        Integer empId = Integer.parseInt((String)data.get("authids["+j+"]"));
                        charemp.setEmpId(empId);
//                        System.out.println("__+++++"+charempp);
                        charempp.add(charemp);

                    }
                }
                if (perm.inschaemp(charempp)){
//                    System.out.println("新增用户角色成功");
                }
            }
        map.put("data","sb");
        return map;
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    //系统设置

    //去届别设置
    @RequestMapping("/tojiebieshe")
    public String tojibieshe(){
        return "jiebieting";
    }

    //届别表的数据
    @RequestMapping("/jiebieshe")
    @ResponseBody
    public Map jiebielit(int page, int limit, Studentfall studentfall){
        Map map = new HashMap();
        List<Studentfall> data = styp.litfall((page - 1) * limit,limit,studentfall);
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",styp.litafall(studentfall));//总页数
        map.put("data",data);//数据
        return map;
    }

    //去添加届别的页面
    @RequestMapping("/toaddjiebie")
    public String toaddjiebie(){
        return "jiebieadd";
    }

    //新增届别
    @RequestMapping("/addjiebie")
    @ResponseBody
    public Map addjiebie(Studentfall studentfall){
        Map map = new HashMap();
        if (styp.addfall(studentfall)){
            map.put("data","添加成功");
        }else {
            map.put("data","添加失败");
        }
        return map;
    }

    //去修改届别页面
    @RequestMapping("/toupjiebie/{fallId}")
    public String toupjiebie(@PathVariable("fallId") Integer fallId,Model model){
        Studentfall studentfall=  styp.slitfall(fallId);
        model.addAttribute("stu",studentfall);
        return "jiebieup";
    }



    //修改届别
    @RequestMapping("/upjiebie")
    @ResponseBody
    public Map upjiebie(Studentfall studentfall){
        Map map = new HashMap();
        if (styp.updfall(studentfall)){
            map.put("data","修改成功");
        }else {
            map.put("data","修改失败");
        }
        return map;
    }
    //删除届别
    @RequestMapping("/deljiebie")
    @ResponseBody
    public Map deljiebie(Integer fallId){
        Map map = new HashMap();
        if (styp.delfall(fallId)){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }

    //--------------------------------------------------------------------------------------------------------------
    //去班级设置
    @RequestMapping("/tobanji")
    public String tobanji(){
        return "banjilis";
    }

    //班级类别表的数据
    @RequestMapping("/banji")
    @ResponseBody
    public Map banji(int page, int limit, Classtype  classtype){
        Map map = new HashMap();
        List<Classtype> data = styp.litclass((page - 1) * limit,limit,classtype);
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",styp.litclasss(classtype));//总页数
        map.put("data",data);//数据
        return map;
    }


    //去添加班级类别的页面
    @RequestMapping("/toaddbanji")
    public String toaddbanji(){
        return "banjiadd";
    }

    //新增班级类别表
    @RequestMapping("/addbanji")
    @ResponseBody
    public Map addjbanji(Classtype classtype){
        Map map = new HashMap();
        if (styp.addclass(classtype)){
            map.put("data","添加成功");
        }else {
            map.put("data","添加失败");
        }
        return map;
    }

    //去修改班级类别表页面
    @RequestMapping("/toupbanji/{calssTypeId}")
    public String toupbanji(@PathVariable("calssTypeId") Integer calssTypeId,Model model){
        Classtype classtype=  styp.slitclass(calssTypeId);
        model.addAttribute("cla",classtype);
        return "banjiup";
    }



    //修改班级类别表
    @RequestMapping("/upbanji")
    @ResponseBody
    public Map upbanji(Classtype classtype){
        Map map = new HashMap();
        if (styp.updeclass(classtype)){
            map.put("data","添加成功");
        }else {
            map.put("data","添加失败");
        }
        return map;
    }
    //删除班级类别
    @RequestMapping("/delbanji")
    @ResponseBody
    public Map delbanji(Integer calssTypeId){
        Map map = new HashMap();
        if (styp.delclass(calssTypeId)){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }

    //--------------------------------------------------------------------------------------------------------------
    //去部门管理界面
    @RequestMapping("/tobumen")
    public String tobumen(Model model){
        List<Dep> deps = perm.litdepduijian();
        TreeView view = new TreeView(deps);
        List<Shuxintu> shuxintus = view.buildTree();
        //将所有部门查出
        List<Dep> lisdep = styp.litdepp();
        List<Emp> lisemp = perm.litempduijian();
//        System.out.println(shuxintus+"sajkdas");
        model.addAttribute("data",JSON.toJSON(shuxintus));
        model.addAttribute("lisdep",lisdep);
        model.addAttribute("lisemp",lisemp);
        return "bumenlis";
    }

    //部门管理表的数据
    @RequestMapping("/bumen")
    @ResponseBody
    public Map bumen(int page, int limit,Dep dep,Model model){

//        System.out.println("<><<<>"+shuxintus);
        Map map = new HashMap();
        List<Map> data = styp.litdep((page - 1) * limit,limit,dep);

        map.put("code",0);
        map.put("msg","成功");
        map.put("count",styp.littdep(dep));//总页数
        map.put("data",data);//数据
        return map;
    }

    //去添加部门页面
    @RequestMapping("/toaddbumenn")
    public String toaddbumenn(Model model){
        //将所有部门查出
        List<Dep> lisdep = styp.litdepp();
        List<Emp> lisemp = perm.litempduijian();
        model.addAttribute("lisdep",lisdep);
        model.addAttribute("lisemp",lisemp);
        return "bumenadd";
    }
    //添加部门
    @RequestMapping("/addbumenn")
    @ResponseBody
    public Map addbumenn(Dep dep){
        Map map = new HashMap();
        System.out.println(dep+"56464");
        if (styp.adddep(dep)){
            map.put("data","添加成功");
        }
        map.put("msg","添加成功");
        return map;
    }

    //去修改部门管理界面
    @RequestMapping("/toupdatebumen/{depid}")
    public String toupdatebumen(@PathVariable("depid") Integer depid, Model model){
        Dep ma = styp.slittdep(depid);
        //将所有部门查出
        List<Dep> lisdep = styp.litdepp();
        List<Emp> lisemp = perm.litempduijian();
        model.addAttribute("ma",ma);
        model.addAttribute("lisdep",lisdep);
        model.addAttribute("lisemp",lisemp);
        return "bumenup";
    }
    //修改部门
    @RequestMapping("/updatebumen")
    @ResponseBody
    public Map updatebumen(Dep dep){
        Map map = new HashMap();
        if (styp.updfdep(dep)){
            map.put("data","修改成功");
        }else {
            map.put("data","修改失败");
        }
        return map;
    }

    //删除部门
    @RequestMapping("/delbumen")
    @ResponseBody
    public Map delbumen(Integer depid, Model model){
        boolean msg = styp.deldep(depid);
        Map map = new HashMap();
        if (msg){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }
    //--------------------------------------------------------------------------------------------------------------
    //去院系管理界面
    @RequestMapping("/toyuanxi")
    public String toyuanxi(){
        return "yuanxilist";
    }

    //院系管理的数据
    @RequestMapping("/yuanxi")
    @ResponseBody
    public Map yuanxi(int page, int limit, Dept dept){
        Map map = new HashMap();
        List<Dept> data = styp.litdept((page - 1) * limit,limit,dept);
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",styp.litdeptt(dept));//总页数
        map.put("data",data);//数据
        return map;
    }


    //去添加院系类别的页面
    @RequestMapping("/toaddyuanxi")
    public String toaddyuanxi(){
        return "yuanxiadd";
    }

    //新增院系类别表
    @RequestMapping("/addyuanxi")
    @ResponseBody
    public Map addyuanxi(Dept dept){
        Map map = new HashMap();
        if (styp.adddept(dept)){
            map.put("data","添加成功");
        }else {
            map.put("data","添加失败");
        }
        return map;
    }

    //去修改院系类别表页面
    @RequestMapping("/toupyuanxii/{deptId}")
    public String toupyuanxi(@PathVariable("deptId") Integer deptId,Model model){
        Dept dept=  styp.slitdept(deptId);
        model.addAttribute("dep",dept);
        return "yuanxiup";
    }



    //修改院系类别表
    @RequestMapping("/upyuanxi")
    @ResponseBody
    public Map upyuanxi(Dept dept){
        Map map = new HashMap();
        if (styp.updedept(dept)){
            map.put("data","修改成功");
        }else {
            map.put("data","修改失败");
        }
        return map;
    }
    //删除院系类别
    @RequestMapping("/delyuanxi")
    @ResponseBody
    public Map delyuanxi(Integer deptId){
        Map map = new HashMap();
        if (styp.deldept(deptId)){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }

    //--------------------------------------------------------------------------------------------------------------
    //去专业表界面
    @RequestMapping("/tozhuanye")
    public String tozhuanye(Model model){
        List<Map> majdept = styp.majdept();
        model.addAttribute("dept",majdept);
        return "zhuanyelist";
    }

    //专业表的数据
    @RequestMapping("/zhuanye")
    @ResponseBody
    public Map zhuanye(int page, int limit, Major major){
        Map map = new HashMap();
        List<Map> data = styp.litmajor((page - 1) * limit,limit,major);
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",styp.litmajorr(major));//总页数
        map.put("data",data);//数据
        return map;
    }


    //去添加专业表的页面
    @RequestMapping("/toaddzhuanye")
    public String toaddzhuanye(Model model){
        List<Map> majdept = styp.majdept();
        model.addAttribute("dept",majdept);
        return "zhuanyeadd";
    }

    //新增专业表
    @RequestMapping("/addzhuanye")
    @ResponseBody
    public Map addzhuanye(Major major){
        Map map = new HashMap();
        if (styp.adddmajor(major)){
            map.put("data","添加成功");
        }else {
            map.put("data","添加失败");
        }
        return map;
    }

    //去修改专业表页面
    @RequestMapping("/toupzhuanye/{majorId}")
    public String toupzhuanyei(@PathVariable("majorId") Integer majorId,Model model){
        Major major=  styp.slitmajort(majorId);
        List<Map> majdept = styp.majdept();
        model.addAttribute("dept",majdept);
        model.addAttribute("ma",major);
        return "zhuanyeup";
    }

    //修改专业表
    @RequestMapping("/upzhuanye")
    @ResponseBody
    public Map upyzhuanye(Major major){
        Map map = new HashMap();
        if (styp.updemajor(major)){
            map.put("data","修改成功");
        }else {
            map.put("data","修改失败");
        }
        return map;
    }
    //删除专业表
    @RequestMapping("/delzhuanye")
    @ResponseBody
    public Map delzhuanye(Integer majorId){
        Map map = new HashMap();
        if (styp.delmajor(majorId)){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }

    //--------------------------------------------------------------------------------------------------------------
    //去答辩项目的界面
    @RequestMapping("/todabian")
    public String todabian(){
        return "dabianlis";
    }

    //答辩项目的数据
    @RequestMapping("/dabian")
    @ResponseBody
    public Map dabian(int page, int limit, Project project){
        Map map = new HashMap();
        List<Project> data = styp.litproject((page - 1) * limit,limit,project);
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",styp.litprojectt(project));//总页数
        map.put("data",data);//数据
        return map;
    }


    //去添加答辩项目
    @RequestMapping("/toadddabian")
    public String toadddabian(Model model){
        return "dabianadd";
    }

    //添加答辩项目
    @RequestMapping("/adddabian")
    @ResponseBody
    public Map adddabian(Project project){
        Map map = new HashMap();
        if (styp.adddproject(project)){
            map.put("data","添加成功");
        }else {
            map.put("data","添加失败");
        }
        return map;
    }

    //去修改答辩项目页面
    @RequestMapping("/toupdabian/{projectId}")
    public String toupdabian(@PathVariable("projectId") Integer projectId,Model model){
        Project Project=  styp.slitproject(projectId);
        model.addAttribute("pro",Project);
        return "dabianup";
    }

    //修改答辩项目
    @RequestMapping("/updabian")
    @ResponseBody
    public Map upydabian(Project project){
        Map map = new HashMap();
        if (styp.updeproject(project)){
            map.put("data","修改成功");
        }else {
            map.put("data","修改失败");
        }
        return map;
    }
    //删除答辩项目
    @RequestMapping("/deldabian")
    @ResponseBody
    public Map deldabian(Integer projectId){
        Map map = new HashMap();
        if (styp.delproject(projectId)){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }
}
